<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends MX_Controller 
{
	function __construct() 
	{
		parent::__construct();	
		$this->load->config('config_site') ;
		$this->load->model('upload_model','upload');
		$this->pre_message="";
	}
	
	function index()
	{
		$data['title'] = "Liên hệ" ;
		$data['page'] = 'upload/index' ;
		$this->load->view('layout/skin', $data);
	}
	
	function action()
	{	
		if(isset($_POST['btnupload']))
		{	
			$this->form_validation->set_rules('cusname','Tên','trim|required');
			$this->form_validation->set_rules('cusmail','Email','trim|required');
			$this->form_validation->set_rules('phone','Phone','trim|required');
			$this->form_validation->set_rules('noidung','Nội dung','trim|required');
			if($this->form_validation->run() == false)
			{
				$this->pre_message = validation_errors();	
			}
			else
			{
				$req = $this->_buildform();
				if($this->upload->save('lienhe_upload',$req))
				{
					$this->session->set_flashdata('message','Lưu thành công');
					redirect(site.'upload/thanhcong');
				}
				else
				{
					$this->pre_message = 'Có lỗi xãy ra, Vui lòng liên hệ qua điện thoại. Cảm ơn';
				}
			}
		}
		
		redirect(site.'upload');
	}
	
	function _buildform($flag=1)
	{
		$req['lb_cusname'] = $this->input->post('cusname');
		$req['lb_cusmail'] = $this->input->post('cusmail');
		$req['nb_phone'] = $this->input->post('phone');
		$req['lb_content'] = $this->input->post('noidung');
		$req['lb_address'] = $this->input->post('address');
		if($flag) $req['dt_create'] = date('Y-m-d H:i:s');
		$req['bl_active'] = 1;
		
		//upload hinh anh
		$img_name = $this->input->post("lb_image");
		if($img_name)
		{
			if(file_exists('uploads/upload/'.$img_name))
			{
				unlink('uploads/upload/'.$img_name);
			}
			@copy('uploads/temp/'.$img_name, 'uploads/upload/'.$img_name);
			$req["lb_image"] = $img_name;
		}
		return $req ;
	}
	
	function thanhcong()
	{
		$data['title'] = "Liên hệ thành công" ;
		$data['cont'] = "Gởi thông tin thành công" ;
		$data['page'] = 'upload/thanhcong' ;
		$this->load->view('layout/skin', $data);
	}
	
	public function picture()
	{
		$this->load->helper('upload') ;
		$dir = "uploads/temp/";
		$res = upload('uploadfile', $dir);
		echo json_encode($res);
	}
}
?>